package org.hitchhike.core.domain;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hitchhike.common.domain.BaseEntity;
import org.hitchhike.core.enums.Constant;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 订单
 * 
 * @author
 *
 */
@Data
@Entity
@Table(name = "tb_order")
@EqualsAndHashCode(callSuper = false)
public class Order extends BaseEntity {

  public Order() {}

  public Order(Long id) {
    super.setId(id);
  }

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(referencedColumnName = "provinceid")
  private Provinces startProvinces;

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(referencedColumnName = "cityid")
  private Cities startCities;

  private String startAddress;// 出发地

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(referencedColumnName = "provinceid")
  private Provinces stopProvinces;

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(referencedColumnName = "cityid")
  private Cities stopCities;

  private String stopAddress;// 目的地

  private String goDate;// 出发日期
  private String goTime;// 出发时间

  /**
   * 提供座位数
   */
  private Integer seatNumber;
  /**
   * 乘车人数
   */
  private Integer rideNumber;

  /**
   * 拼车价格
   */
  private Integer price1;
  /**
   * 不拼车价格
   */
  private Integer price2;
  /**
   * 是否拼车
   */
  private Boolean carpool;

  /**
   * 其它说明
   */
  private String other;

  /**
   * 订单类型
   */
  private int orderType;
  /**
   * 订单状态
   */
  private int orderState;
  /**
   * 订单流程
   */
  private int orderProcess;

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "user_id")
  private User user;

  @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
  private List<OrderDetail> orderDetails;

  @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
  private List<Comment> comments;

  /**
   * 预定数量
   * 
   * @return
   */
  public int getPredeterminedNumber() {
    return this.orderDetails.size();
  }

  @Transient
  private String goHH;

  @Transient
  private String goMM;

  public String getOrderStateName() {
    String orderStateName = null;
    switch (this.orderState) {
      case Constant.ORDER_STATE_0:
        orderStateName = "取消";
        break;
      case Constant.ORDER_STATE_1:
        orderStateName = "待定中";
        break;
      case Constant.ORDER_STATE_2:
        orderStateName = "进行中";
        break;
      case Constant.ORDER_STATE_3:
        orderStateName = "完成";
        break;
      default:
        break;
    }
    return orderStateName;
  }

  public String getOrderProcessName() {
    String orderProcess = null;
    switch (this.orderProcess) {
      case Constant.ORDER_PROCESS_0:
        orderProcess = "未接单";
        break;
      case Constant.ORDER_PROCESS_1:
        orderProcess = "已接单";
        break;
      case Constant.ORDER_PROCESS_2:
        orderProcess = "待付款";
        break;
      case Constant.ORDER_PROCESS_3:
        orderProcess = "待评价";
        break;
      case Constant.ORDER_PROCESS_4:
        orderProcess = "已评价";
        break;
      default:
        break;
    }
    return orderProcess;
  }

}
